/*
Copyright 2009 Homosuperiorus LLC 

Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

*/

package org.taglets.pub.account;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.taglets.exception.RateLimitExceededException;
import org.taglets.exception.ServerErrorException;
import org.taglets.exception.UnknownUserException;
import org.taglets.pub.AppResponse;
import org.taglets.pub.HttpResponse;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class RetrieveResponse extends AppResponse {
    private final String USER_TAG = "/user";
    private TagletsUser user;

    public RetrieveResponse(HttpResponse httpResponse) throws IOException, ServerErrorException, UnknownUserException, RateLimitExceededException {
        super(httpResponse);
        
        /*
         * <?xml version="1.0" encoding="UTF-8"?>
         * <user created="2009-01-06 03:17:14.968949" email="mark@carpediemcg.com" firstname="" lastname="" state="1" twitterid="wn0owp" twitterstate="1"/>
         */
        if (responseCode == HttpURLConnection.HTTP_OK) {
            succeeded = true;
            try {
                DocumentBuilder builder;
                builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document document = builder.parse(getInputStream());
                XPath xpath = XPathFactory.newInstance().newXPath();
                Element tag = (Element) xpath.evaluate(USER_TAG, document, XPathConstants.NODE);
                String email = xpath.evaluate(USER_TAG + "/@email", tag);
                String lastName = xpath.evaluate(USER_TAG + "/@lastname", tag);
                String firstName = xpath.evaluate(USER_TAG + "/@firstname", tag);
                int state = Integer.parseInt(xpath.evaluate(USER_TAG + "/@state", tag));
                String twitterId = xpath.evaluate(USER_TAG + "/@twitterid", tag);
                int twitterState = Integer.parseInt(xpath.evaluate(USER_TAG + "/@twitterstate", tag));
                String created = xpath.evaluate(USER_TAG + "/@created", tag);
                user = new TagletsUser(email, null, lastName, firstName, state, twitterId, twitterState, created);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
        } else if (responseCode == HttpURLConnection.HTTP_PRECON_FAILED) {
            throw new UnknownUserException();
        } else if (responseCode == HttpURLConnection.HTTP_GONE) {
            throw new RateLimitExceededException();
        } else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
            throw new ServerErrorException();
        }
    }

    public void setUser(TagletsUser user) {
        this.user = user;
    }

    public TagletsUser getUser() {
        return user;
    }

}
